#include "EpilFunctionStatementParser.h"
#include "EpilFunctionNameParser.h"
#include "EpilFunctionBodyParser.h"
#include "EpilPointerHelper.h"


EpilFunctionStatementParser::EpilFunctionStatementParser( EpilTokenStream & tokens )
: EpilSyntaxParserBase( tokens )
{
}


void EpilFunctionStatementParser::Parse()
{
	// funcstat -> FUNCTION funcname body
	m_functionStatement = new EpilFunctionStatementCode( this->GetTokens().GetCurrentToken().location );

	// FUNCTION
	this->GetTokens().MoveToNext();

	// funcname
	EpilFunctionNameParser funcNameParser( this->GetTokens() );
	try
	{
		funcNameParser.Parse();
	}
	catch( ... )
	{
		EpilPointerHelper::SafeDelete( m_functionStatement );
		throw;
	}
	m_functionStatement->SetFunctionName( funcNameParser.GetFunctionName() );

	// body
	EpilFunctionBodyParser funcBodyParser( this->GetTokens() );
	try
	{
		funcBodyParser.Parse();
	}
	catch( ... )
	{
		EpilPointerHelper::SafeDelete( m_functionStatement );
		throw;
	}
	m_functionStatement->SetBody( funcBodyParser.GetBody() );
}